/**
 * 
 */
package org.cuatrovientos.erp.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.cuatrovientos.erp.business.CountryBOInterface;
import org.cuatrovientos.erp.model.Country;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.beans.factory.annotation.Autowired;



/**
 * Controller class form Country management
 * @author pablo_guerrero
 */
@Controller
public class CountryController {

	// Get log4j logger
	private static final Logger logger = Logger.getLogger(CountryController.class);

	@Autowired
    private CountryBOInterface countryBO;
    
        /**
         * default constructor
         */
        public CountryController () {
               logger.info("Country BEAN instantiated");
                
        }
        
        
        
        /**
         * handles default /country or /country/list request
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/country","/country/list"})
         */
        @RequestMapping({"/country","/country/list"})
        public ModelAndView countryDefault() {
        	logger.info("default");
 
                Map<String, Object> model = new HashMap<String, Object>();

                List<Country> countryList = countryBO.selectAll();
                model.put("countries", countryList );
                return new ModelAndView("CountryList", model);     
        }

        /**
         * handles /country/search request, redirected to default page
         * @param model
         * @return the name of the view to show
         * @RequestMapping({"/country/search"})
         */
        @RequestMapping(method=RequestMethod.POST,value={"/country/search"})
        public ModelAndView search(@RequestParam(value="description", defaultValue="", required=true) String description) {
        	logger.info("search");
 
            Map<String, Object> model = new HashMap<String, Object>();

            List<Country> countryList = countryBO.search(description);
            model.put("countries", countryList );
            
            return new ModelAndView("CountryList", model);     
        }

        /**
         * handles delete country, and redirect to country default
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/country","/country/list"})
         */
        @RequestMapping(method=RequestMethod.GET,value={"/country/delete"})
        public String deleteCountry(@RequestParam(value="id", defaultValue="", required=true) int id) {
        	logger.info("delete " + id);

        	countryBO.deleteById(id);
            return "redirect:/country";

        }
        
        /**
         * handles new country request. In the jsp we are using spring's form:form tags
         * Spring expects a model called by default 'command' or any name
         * we set to it: in this case "country"
         * @return the name of the view to show
         * @RequestMapping({"/country/new"})
         */
        @RequestMapping(method=RequestMethod.GET,value={"/country/new"})
        public ModelAndView newCountry() {
        		logger.info("new country ");
                Country country = new Country();
               
                return new ModelAndView("CountryNew", "country", country);
        }
        
        /**
         * handles new country request. In the jsp we are using spring's form:form tags
         * Spring expects a model called by default 'command' or any name
         * we set to it.
         * @return the name of the view to show
         * @RequestMapping({"/country/new"})
         */
        @RequestMapping(method=RequestMethod.POST,value={"/country/save"})
        public String newCountry(@Valid Country country, BindingResult bindingResult) {
        		logger.info("new country " + country.toString());
        		
        		// In case of validation errors, go back to form
        		if (bindingResult.hasErrors()) {
        			Map<String, Object> model = new HashMap<String, Object>();
        			model.put("country", country);
        			return "CountryNew";
        		}
        		
        		// If data is ok, insert and go on
                countryBO.insert(country);
                return "redirect:/country";
        }
        
        /**
         * handles delete country, and redirect to country default
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/country","/country/list"})
         */
        @RequestMapping(method=RequestMethod.GET,value={"/country/detail"})
        public ModelAndView countryDetail(@RequestParam(value="id", defaultValue="", required=true) int id) {
        	logger.info("Detail " + id);
            Map<String,Object> model = new HashMap<String,Object>();
            Country country = countryBO.selectById(id);
            model.put("country", country);
            
            return new ModelAndView("CountryDetail",model);

        }
        
        /**
         * handles country update, first part: register loaded
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/country/update"})
         */
        @RequestMapping(method=RequestMethod.GET,value={"/country/update"})
        public ModelAndView countryUpdate(@RequestParam(value="id", defaultValue="", required=true) int id) {
        	logger.info("update " + id);
                Map<String,Object> model = new HashMap<String,Object>();
                Country country = countryBO.selectById(id);
                model.put("country", country);
               return new ModelAndView("CountryUpdate",model);

        }
        
        /**
         * handles save update country request. In the jsp we are using spring's form:form tags
         * Spring expects a model called by default 'command' or any name
         * we set to it.
         * @return the name of the view to show
         * @RequestMapping({"/country/saveUpdate"})
         */
        @RequestMapping(method=RequestMethod.POST,value={"/country/saveupdate"})
        public String saveUpdate(@Valid Country country, BindingResult bindingResult) {
        		logger.info("update country " + country.toString());
        		
        		// In case of validation errors, go back to form
        		if (bindingResult.hasErrors()) {
        			Map<String, Object> model = new HashMap<String, Object>();
        			model.put("country", country);
        			return "CountryUpdate";
        		}
        		
        		countryBO.update(country);
                return "redirect:/country";
        }
}
